<template>
	<view class="wallet-container">
		<!-- 余额展示 -->
		<view class="balance-section text-center">
			<view class="balance-title">我的余额</view>
			<view class="balance-amount">
				<text class="currency">¥</text>
				<text class="amount">{{walletInfo.balance || '0.00'}}</text>
			</view>
			<view class="balance-detail">
				<view class="detail-item">
					<text class="label">可用余额</text>
					<text class="value">¥{{walletInfo.availableBalance || '0.00'}}</text>
				</view>
				<view class="detail-item">
					<text class="label">冻结余额</text>
					<text class="value frozen">¥{{walletInfo.frozenBalance || '0.00'}}</text>
					<text class="frozen-tip" @tap="showFrozenDetail">>></text>
				</view>
			</view>
		</view>

		<!-- 操作按钮 -->
		<view class="action-buttons">
			<button class="action-btn recharge" @tap="handleRecharge">充值</button>
			<button class="action-btn withdraw" @tap="handleWithdraw">提现</button>
		</view>

		<!-- 交易记录 -->
		<view class="transaction-section">
			<view class="section-header">
				<text class="title">交易记录（{{walletInfo.transactionCount}}）</text>
				<text class="more" @tap="goToTransactionList">查看全部 ></text>
			</view>
		</view>

		<!-- 充值弹框 -->
		<uni-popup ref="rechargePopup" type="center">
			<view class="recharge-popup">
				<view class="popup-header">
					<text class="title">充值</text>
					<text class="close" @tap="closeRechargePopup">×</text>
				</view>
				<view class="popup-content">
					<view class="input-wrap">
						<text class="currency">¥</text>
						<input type="digit" v-model="rechargeAmount" class="amount-input" placeholder="请输入充值金额"
							@input="handleAmountInput" />
					</view>
					<view class="quick-amounts">
						<view class="amount-item" :class="{ active: rechargeAmount === amount }"
							v-for="amount in quickAmounts" :key="amount" @tap="selectAmount(amount)">
							¥{{ amount }}
						</view>
					</view>
				</view>
				<view class="popup-footer">
					<button class="confirm-btn" :disabled="!isValidAmount" @tap="confirmRecharge">
						确认充值
					</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		my
	} from '@/api/wallet/wallet.js'
	import {
		addRechargeOrder
	} from '@/api/rechargeOrder/rechargeOrder.js'
	export default {
		data() {
			return {
				walletInfo: {
					balance: '0.00',
					availableBalance: '0.00',
					frozenBalance: '0.00'
				},
				rechargeAmount: '', // 充值金额
				quickAmounts: ['0.01', '50', '100', '200', '500'], // 快捷金额选项
			}
		},
		computed: {
			isValidAmount() {
				const amount = parseFloat(this.rechargeAmount)
				return amount && amount > 0 && amount <= 10000 // 限制最大充值金额为10000
			}
		},
		onLoad() {
			this.getWalletInfo()
		},
		onShow() {
			this.getWalletInfo()
		},
		methods: {
			// 获取钱包信息
			async getWalletInfo() {
				try {
					const res = await my()
					if (res.code === 200) {
						this.walletInfo = res.data
					}
				} catch (error) {
					console.error('获取钱包信息失败:', error)
				}
			},
			// 充值
			handleRecharge() {
				this.rechargeAmount = ''
				this.$refs.rechargePopup.open()
			},
			// 提现
			handleWithdraw() {
				uni.navigateTo({
					url: '/pages/wallet/withdraw'
				})
			},
			// 查看冻结详情
			showFrozenDetail() {
				uni.navigateTo({
					url: '/pages/wallet/frozen-list'
				})
			},
			// 查看完整交易记录
			goToTransactionList() {
				uni.navigateTo({
					url: '/pages/wallet/transaction-list'
				})
			},
			// 关闭充值弹框
			closeRechargePopup() {
				this.$refs.rechargePopup.close()
			},
			// 处理金额输入
			handleAmountInput(e) {
				let value = e.detail.value
				// 限制只能输入数字和小数点，且最多两位小数
				value = value.replace(/[^\d.]/g, '')
				if (value.indexOf('.') > -1) {
					value = value.slice(0, value.indexOf('.') + 3)
				}
				this.rechargeAmount = value
			},
			// 选择快捷金额
			selectAmount(amount) {
				this.rechargeAmount = amount
			},
			// 确认充值
			confirmRecharge() {
				let _this = this
				if (!_this.isValidAmount) return

				// TODO: 调用创建充值订单接口
				const params = {
					amount: parseFloat(_this.rechargeAmount)
				}

				addRechargeOrder(params).then(res => {
					console.log('addRechargeOrder', res)
					if (res.code === 200) {
						console.log('addRechargeOrder', res)
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data.packageValue,
							signType: res.data.signType,
							paySign: res.data.paySign,
							success: function(payres) {
								console.log('success:' + JSON.stringify(payres));
								_this.getWalletInfo()
							},
							fail: function(err) {
								console.log('fail:' + JSON.stringify(err));
							}
						})

					}
				}).catch(err => {
					uni.showToast({
						title: err.message || '创建充值订单失败',
						icon: 'none'
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.wallet-container {
		min-height: 100vh;
		background: #f5f5f5;
		padding: 20rpx;

		.balance-section {
			background: #fff;
			border-radius: 12rpx;
			padding: 40rpx 30rpx;
			margin-bottom: 20rpx;

			&.text-center {
				text-align: center;
			}

			.balance-title {
				font-size: 28rpx;
				color: #666;
				margin-bottom: 20rpx;
			}

			.balance-amount {
				margin-bottom: 30rpx;
				display: flex;
				justify-content: center;
				align-items: baseline;

				.currency {
					font-size: 40rpx;
					color: #333;
				}

				.amount {
					font-size: 60rpx;
					font-weight: bold;
					color: #333;
				}
			}

			.balance-detail {
				.detail-item {
					display: inline-flex;
					align-items: center;
					justify-content: center;
					margin-bottom: 16rpx;
					margin: 0 20rpx 16rpx;

					.label {
						font-size: 26rpx;
						color: #666;
						margin-right: 20rpx;
					}

					.value {
						font-size: 26rpx;
						color: #333;

						&.frozen {
							color: #ff6b35;
						}
					}

					.frozen-tip {
						font-size: 24rpx;
						color: #ff6b35;
						margin-left: 10rpx;
					}
				}
			}
		}

		.action-buttons {
			display: flex;
			justify-content: space-between;
			padding: 20rpx 0;

			.action-btn {
				width: 48%;
				height: 88rpx;
				line-height: 88rpx;
				border-radius: 44rpx;
				font-size: 32rpx;

				&.recharge {
					background: #4CAF50;
					color: #fff;
				}

				&.withdraw {
					background: #ff6b35;
					color: #fff;
				}
			}
		}

		.transaction-section {
			background: #fff;
			border-radius: 12rpx;
			padding: 30rpx;

			.section-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 20rpx;

				.title {
					font-size: 32rpx;
					font-weight: bold;
					color: #333;
				}

				.more {
					font-size: 26rpx;
					color: #666;
				}
			}
		}

		.recharge-popup {
			width: 600rpx;
			background: #fff;
			border-radius: 12rpx;

			.popup-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 30rpx;
				border-bottom: 1rpx solid #eee;

				.title {
					font-size: 32rpx;
					font-weight: bold;
					color: #333;
				}

				.close {
					font-size: 40rpx;
					color: #999;
					padding: 0 20rpx;
				}
			}

			.popup-content {
				padding: 30rpx;

				.input-wrap {
					display: flex;
					align-items: center;
					padding: 20rpx 0;
					border-bottom: 1rpx solid #eee;
					margin-bottom: 30rpx;

					.currency {
						font-size: 40rpx;
						color: #333;
						margin-right: 20rpx;
					}

					.amount-input {
						flex: 1;
						font-size: 32rpx;
					}
				}

				.quick-amounts {
					display: flex;
					flex-wrap: wrap;
					gap: 20rpx;

					.amount-item {
						width: calc(50% - 10rpx);
						height: 80rpx;
						line-height: 80rpx;
						text-align: center;
						background: #f5f5f5;
						border-radius: 8rpx;
						font-size: 28rpx;
						color: #333;

						&.active {
							background: #ff6b35;
							color: #fff;
						}
					}
				}
			}

			.popup-footer {
				padding: 30rpx;

				.confirm-btn {
					width: 100%;
					height: 88rpx;
					line-height: 88rpx;
					background: #4CAF50;
					color: #fff;
					border-radius: 44rpx;
					font-size: 32rpx;

					&[disabled] {
						opacity: 0.5;
					}
				}
			}
		}
	}
</style>